﻿using Newtonsoft.Json;
using Padsurface.Audio.Song.Model;
using Padsurface.Audio.Song.Repositories.Interfaces;
using System;
using System.Composition;
using System.Threading.Tasks;
using Windows.Storage;

namespace Padsurface.Audio.Song.Repositories
{
    [Export(typeof(ISongRepository))]
    internal class SongRepository : ISongRepository
    {
        public async Task Save(SerializableSong song, StorageFile storageFile)
        {
            if (song == null) throw new ArgumentNullException("song");
            if (storageFile == null) throw new ArgumentNullException("storageFile");

            var serializedSong = JsonConvert.SerializeObject(song);

            await FileIO.WriteTextAsync(storageFile, serializedSong);
        }

        public async Task<SerializableSong> Get(StorageFile storageFile)
        {
            if (storageFile == null) throw new ArgumentNullException("storageFile");

            var fileContents = await Windows.Storage.FileIO.ReadTextAsync(storageFile);

            return JsonConvert.DeserializeObject<SerializableSong>(fileContents);
        }
    }
}
